using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;
using System.Collections;

namespace DAO
{
    public class TrongTaiDAO
    {
        #region DanhSach
        public IList DanhSach()
        {
            SqlConnection connect = SqlDataAccessHelper.ConnectSQL();
            SqlCommand command = connect.CreateCommand();
            command.CommandText = "SELECT * FROM TRONGTAI";
            SqlDataReader dr = command.ExecuteReader();
            IList list = new ArrayList();
            while (dr.Read())
            {
                TrongTaiDTO trongtai = new TrongTaiDTO();
                trongtai.MaTrongTai = (int)dr["MaTrongTai"];
                trongtai.TrongTai = (int)dr["TrongTai"];
                list.Add(trongtai);
            }
            dr.Close();
            connect.Close();
            TrongTaiDTO[] listTrongTai = new TrongTaiDTO[list.Count];
            list.CopyTo(listTrongTai, 0);
            return listTrongTai;
        }
            
        #endregion
        #region Retrieving
        public static List<TrongTaiDTO> SelectTrongTaiAll()
        {
            List<TrongTaiDTO> list = new List<TrongTaiDTO>();
            try
            {
                DataTable dt = SqlDataAccessHelper.ExecuteQuery("spSelectTrongTaiAll");
                foreach(DataRow dr in dt.Rows)
                {
                    TrongTaiDTO trongtai = new TrongTaiDTO();
                    trongtai.MaTrongTai = int.Parse(dr["MaTrongTai"].ToString());
                    trongtai.TrongTai = int.Parse(dr["TrongTai"].ToString());
                    list.Add(trongtai);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return list;
        }

        public static TrongTaiDTO SelectTrongTaiByMa(int MaTrongTai)
        {
            TrongTaiDTO trongtai = new TrongTaiDTO();
            try
            {
                //Create List Sql Parameter
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaTrongTai", MaTrongTai));
                DataTable dt = SqlDataAccessHelper.ExecuteQuery("spSelectTrongTaiByMa", sqlParams);
                DataRow dr = dt.Rows[0];
                trongtai.MaTrongTai = int.Parse(dr["MaTrongTai"].ToString());
                trongtai.TrongTai = int.Parse(dr["TrongTai"].ToString());

            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            return trongtai;
        }

        public static bool CheckTrongTaiByMa(int MaTrongTai)
        {
            bool result = false;
            try
            {
                //Create List Sql Parameter
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaTrongTai", MaTrongTai));

                DataTable dt = SqlDataAccessHelper.ExecuteQuery("spSelectTrongTaiByMa", sqlParams);
                if (dt.Rows.Count == 1)
                {
                    result = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
        #endregion
    }
}
